package com.example.supersbus;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.base.BaseFragment;
import com.example.fragment.FavoriteFragment;
import com.example.fragment.HomeFragment;
import com.example.fragment.MoreFragment;
import com.example.fragment.SurroundingFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private RadioGroup rg;
    private List<BaseFragment> mBaseFragment;
    //选择的fragment对应的位置
    private int position;
    //上次切换的fragment
    private Fragment mContent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //初始化VIew
        initView();
        //初始化fragemnt
        initFragment();
        setListener();
    }

    private void setListener() {
        rg.setOnCheckedChangeListener(new MyOnCheckedChangeListener());
        rg.check(R.id.rb_home);
    }

    class MyOnCheckedChangeListener implements RadioGroup.OnCheckedChangeListener{

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId){
                case R.id.rb_home:
                    position = 0;
                    break;
                case R.id.rb_favorite:
                    position = 1;
                    break;
                case R.id.rb_surrounding:
                    position = 2;
                    break;
                case R.id.rb_more:
                    position = 3;
                    break;
                default:
                    position = 0;
                    break;
            }

            //根据位置替换fragment
            BaseFragment to = getFragment();
            //替换fragment
            switchFragment(mContent, to);
            
        }
    }

    /**
     *
     * @param from 刚显示的fragment马上就要隐藏
     * @param to 马上要切换到的fragment，一会要显示
     */
    private void switchFragment(Fragment from, Fragment to) {
        if(from != to){
            mContent = to;
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            //才切换
            //判断有没有被添加
            if(!to.isAdded()){
                //to没有被添加
                //from隐藏
                if(from != null){
                    ft.hide(from);
                }
                //添加to
                if(to != null){
                    ft.add(R.id.fragment_container,to).commit();
                }
            }else{
                //to已经被添加
                // from隐藏
                if(from != null){
                    ft.hide(from);
                }
                //显示to
                if(to != null){
                    ft.show(to).commit();
                }
            }
        }
    }

    //根据位置得到fragment
    private BaseFragment getFragment() {
        BaseFragment baseFragment = mBaseFragment.get(position);
        return baseFragment;
    }

    private void initFragment() {
        mBaseFragment = new ArrayList<>();

        mBaseFragment.add(new HomeFragment());
        mBaseFragment.add(new FavoriteFragment());
        mBaseFragment.add(new SurroundingFragment());
        mBaseFragment.add(new MoreFragment());

    }

    private void initView(){
        setContentView(R.layout.activity_main);
        rg = findViewById(R.id.rg_root);
    }

}
